﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace TiebaSignWP7
{
    class TiebaListGetter
    {
        public TiebaListGetter(CookieContainer cc)
        {
            this.tcc = cc;
        }

        private CookieContainer tcc = new CookieContainer();
        private string listUrl = "http://tieba.baidu.com/f/like/mylike?&pn=";
        private int pageNumber = 1;
        private bool isPageEmpty = false;
        private bool isAsyncComplete = false;
        List<string> ls = new List<string>();

        internal List<string> GetBlockList()
        {
            GetPage(listUrl + pageNumber);
            while (isPageEmpty == false)
            {
                while (isAsyncComplete == true)
                {
                    isAsyncComplete = false;
                    pageNumber++;
                    GetPage(listUrl + pageNumber);
                }
            }
            return ls;
        }

        internal List<string> GetDetailListFromBlockList(List<string> blockList)
        {
            List<string> detailList = RegularDetail(blockList);
            return detailList;
        }


        private void GetPage(string tiebaUrl)
        {
            string url = tiebaUrl;
            //创建WebRequest类
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            if (tcc == null)
                tcc = new CookieContainer();
            request.CookieContainer = tcc;
            request.Method = "GET";
            //返回异步操作的状态
            IAsyncResult result = (IAsyncResult)request.BeginGetResponse(GetPageResponse, request);
        }

        private void GetPageResponse(IAsyncResult result)
        {
            string webPageContentString = "";
            //获取异步操作返回的的信息
            HttpWebRequest request = (HttpWebRequest)result.AsyncState;
            //结束对 Internet 资源的异步请求
            WebResponse response = request.EndGetResponse(result);
            //avoid multi-dispose by try finally segment
            Stream strm = null;
            try
            {
                strm = response.GetResponseStream();
                using (StreamReader sreader = new StreamReader(strm, b_wind.Text.GB18030Encoding.GB18030))
                {
                    strm = null;
                    webPageContentString = sreader.ReadToEnd();
                    HandlePageException(webPageContentString);
                }
            }
            finally
            {
                if (strm != null)
                    strm.Dispose();
            }
        }

        private void HandlePageException(string webPageContentString)
        {
            if (webPageContentString.Contains("balvid"))
            {
                Regular(webPageContentString);
            }
            else
                isPageEmpty = true;
        }

        private void Regular(string webPageContentString)
        {
            //string rs = "(title=\")(.*?)(</a></td><td><a class=\"cur_exp\")"
            //    + "(.*?)(lv_nav_intro\">)(.*?)"
            //    + "(</a></td><td><a style=\"display:block;\" class=\"like_badge badge_lv2_2\" title=\")"
            //    + "(.*?)(\" target=)(.*?)(like_badge_title\">)(.*?)"
            //    + "(</div><div class=\"like_badge_lv\">)(.*?)"
            //    + "(balvid=\")(.*?)(\" balvname=\")(.*?)(\")";
            string rs = "(title=\")(.*?)(tbs=\")";
            Regex r = new Regex(rs, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            Match m = r.Match(webPageContentString);

            for (; m.Success; m = m.NextMatch())
            {
                //ls.Add(m.Groups[16].ToString() + "," + m.Groups[18].ToString() + ","
                //    + m.Groups[2].ToString() + ","
                //    + m.Groups[6].ToString() + "," + m.Groups[8].ToString() + ","
                //    + m.Groups[12].ToString());
                ls.Add(m.Groups[2].ToString());
            }
            isAsyncComplete = true;
        }

        private List<string> RegularDetail(List<string> blockList)
        {
            List<string> dList = new List<string>();
            string rs = "(.*?)(</a></td><td><a class=\"cur_exp\")"
                + "(.*?)(lv_nav_intro\">)(.*?)(</a></td>)(.*?)(title=\")"
                + "(.*?)(\" target=)(.*?)(like_badge_title\">)(.*?)"
                + "(</div><div class=\"like_badge_lv\">)(.*?)"
                + "(balvid=\")(.*?)(\" balvname=\")(.*?)(\" )";
            Regex r = new Regex(rs, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            foreach (string s in blockList)
            {
                Match m = r.Match(s);
                for (; m.Success; m = m.NextMatch())
                {
                    dList.Add(m.Groups[1].ToString().Replace("\">", "^").Split('^')[1] + ","
                        + m.Groups[5].ToString() + ","
                        + m.Groups[9].ToString() + ","
                        + m.Groups[13].ToString() + ","
                        + m.Groups[17].ToString() + ","
                        + m.Groups[19].ToString());
                }
            }
            return dList;
        }
    }
}
